import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Workflows from '../views/Workflows.vue'
import Tasks from '../views/Tasks.vue'
import WorkflowDetail from '../views/WorkflowDetail.vue'
import WorkflowTemplates from '../views/WorkflowTemplates.vue'
import WorkflowEditor from '../views/WorkflowEditor.vue'
import WorkflowTemplateDetail from '../views/WorkflowTemplateDetail.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/workflows',
    name: 'Workflows',
    component: Workflows
  },
  {
    path: '/tasks',
    name: 'Tasks',
    component: Tasks
  },
  {
    path: '/workflow/:id',
    name: 'WorkflowDetail',
    component: WorkflowDetail
  },
  {
    path: '/workflow-templates',
    name: 'WorkflowTemplates',
    component: WorkflowTemplates
  },
  {
    path: '/workflow-editor',
    name: 'WorkflowEditorCreate',
    component: WorkflowEditor
  },
  {
    path: '/workflow-editor/:id',
    name: 'WorkflowEditorEdit',
    component: WorkflowEditor
  },
  {
    path: '/workflow-template/:id',
    name: 'WorkflowTemplateDetail',
    component: WorkflowTemplateDetail
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 